package cn.laixueit.listener;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.stereotype.Component;

@Component
public class SmsUtils {

    public Boolean sendSms(String phone,String code) throws Exception {
        try {
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId("LTAI4GBm8U2SQe9kncWiWof8")
                    // 您的AccessKey Secret
                    .setAccessKeySecret("D0wHwgZFmhgr2ULFVP9Oa8R40vcSn3");
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";
            Client client = new Client(config);
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName("laixueit")
                    .setTemplateCode("SMS_190271990")
                    .setTemplateParam("{\"code\":\""+code+"\"}");
            // 复制代码运行请自行打印 API 的返回值
            client.sendSms(sendSmsRequest);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
